package com.ui.containers
{
	import com.net.AssetData;
	import com.ui.controls.Label;
	import com.ui.core.ScaleMode;
	import com.ui.core.UIComponent;
	import com.ui.data.TitleBarData;
	import com.ui.layout.GLayout;
	import com.ui.manager.UIManager;
	import com.ui.skin.SkinStyle;
	import com.utils.BDUtil;

	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Shape;

	com.ui.controls.Label;
	/**
	 * @version 20091215
	 * @author Cafe
	 */
	public class TitleBar extends UIComponent
	{
		protected var _data : TitleBarData;
		protected var _backgroundSkin : DisplayObject;
		protected var _label : Label;
		protected var _layout : GLayout;
		private var _centL : Bitmap;
		private var _left : Bitmap;
		private var _right : Bitmap;

		override protected function create() : void
		{
			resetBG();
			_data.backgroundAsset = new AssetData(SkinStyle.emptySkin);
			_backgroundSkin = UIManager.getUI(_data.backgroundAsset);
			_data.labelData.textFormat.bold = true;
			_data.labelData.textFormat.size = 15;
			_label = new Label(_data.labelData);
			//addChild(_backgroundSkin);
			addChild(_label);
			// GLayout.layout( _label );
			_label.x = _data.width / 2 - _label.width / 2;
			switch(_data.scaleMode)
			{
				case ScaleMode.SCALE_WIDTH:
					_height = _backgroundSkin.height;
					break;
				case ScaleMode.SCALE_NONE:
					_width = _backgroundSkin.width;
					_height = _backgroundSkin.height;
					break;
			}
		}

		public function resetBG() : void
		{
			var myShape : Shape = new Shape();
			myShape.graphics.beginFill(0xFFFFFF, 0);
			myShape.graphics.drawRect(0, -5, _data.width, _data.height + 5);
			myShape.graphics.endFill();
			this.addChild(myShape);
			if (_data.width <= 300)
			{
				addSmallBg();
			}
			else if(_data.width <= 500)
			{
				addBigBg();
			}
			else
			{
				addBigBg();
				addMiddleBg();
			}
			if (_label)
				_label.x = _data.width / 2 - _label.width / 2;
		}
		
		private function addMiddleBg():void
		{
			_centL = new Bitmap();
			_centL.bitmapData = BDUtil.getBD(new AssetData("middle_title"));
			addChildAt(_centL, 0);
			_centL.y = -6;
			_centL.x = _data.width / 2 - _centL.width / 2;
		}
		
		private function addBigBg():void
		{
			if (_left == null)
			{
				_left = new Bitmap();
				addChildAt(_left, 0);
			}
			_left.bitmapData = BDUtil.getBD(new AssetData("bleft_title"));
			if (_right == null)
			{
				_right = new Bitmap();
				addChildAt(_right, 0);
			}
			_right.bitmapData = BDUtil.getBD(new AssetData("bright_title"));
			_left.x = 2;
			_left.y = -5;
			_right.y = -5;
			_right.x = _data.width - _right.width - 2;
			
		}
		
		private function addSmallBg():void
		{
			if (_left == null)
			{
				_left = new Bitmap();
				addChildAt(_left, 0);
			}
			_left.bitmapData = BDUtil.getBD(new AssetData("sleft_title"));
			if (_right == null)
			{
				_right = new Bitmap();
				addChildAt(_right, 0);
			}
			_right.bitmapData = BDUtil.getBD(new AssetData("sright_title"));
			_left.x = 2;
			_left.y = -4;
			_right.y = -4;
			_right.x = _data.width - _right.width - 2;
		}
		
		override protected function layout() : void
		{
			switch(_data.scaleMode)
			{
				case ScaleMode.SCALE9GRID:
					_backgroundSkin.width = _width;
					_backgroundSkin.height = _height;
					break;
				case ScaleMode.SCALE_WIDTH:
					_backgroundSkin.width = _width;
					break;
				case ScaleMode.SCALE_NONE:
					break;
				default:
					// Logger.error("scale mode is invalid!");
					break;
			}
			// GLayout.layout(_label);
		}

		public function TitleBar(data : TitleBarData)
		{
			_data = data;
			super(data);
		}

		public function set text(value : String) : void
		{
			_label.text = value;
			// GLayout.layout( _label );
		}

		public function get label() : Label
		{
			return _label;
		}
		
		public function get centL():Bitmap
		{
			return _centL;
		}
		
		public function get left():Bitmap
		{
			return _left;
		}
		
		public function get right():Bitmap
		{
			return _right;
		}
	}
}
